<?php

namespace app\admin\controller;

use app\admin\model\AdminLog;
use app\common\controller\Backend;
use app\common\library\Platform;
use app\common\model\zbtask\Import;
use app\common\model\zbtask\Money;
use app\common\model\zbtask\Zbtask;
use think\Config;
use think\Hook;
use think\Session;
use think\Validate;

/**
 * 后台首页
 * @internal
 */
class Index extends Backend
{

    protected $noNeedLogin = ['login'];
    protected $noNeedRight = ['index', 'logout'];
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 后台首页
     */
    public function index()
    {
        if (TYPE == 0) {
            $zbtaskMoney = Money::where(['type' => 0, 'status'=> 1])->count(); //待审核众包费
            $noImport = Import::where(['is_import' => 0])->count(); //待审核众包费
            $task = Zbtask::where(['type' => 0, 'status'=> 1])->count();
            $allcount = $zbtaskMoney + $noImport + $task;
            //总平台管理员
            list($menulist, $navlist, $fixedmenu, $referermenu) = $this->auth->getSidebar([
                'addon'     => ['new', 'red', 'badge'],
                'auth/rule' => __('Menu'),
                'zbtask' =>  ['待处理:'.$allcount , 'red', 'badge'],
                'zbtask/zbtask' => ['待审核:'.$task , 'red', 'badge'],
                'zbtask/money'   => ['待审核:'.	$zbtaskMoney . ',未导入:' .$noImport, 'red', 'badge'],
            ], $this->view->site['fixedpage']);
        } else if (TYPE == 1) {
            $com_where['company_id'] = TYPE_ID;
            $zbtaskMoney = Money::where(['type' => 0, 'status'=> 1])->where($com_where)->count(); //待审核众包费
            $task = Zbtask::where(['type' => 0, 'status'=> 1])->where($com_where)->count();
            $allcount = $zbtaskMoney  + $task;
            //总平台管理员
            list($menulist, $navlist, $fixedmenu, $referermenu) = $this->auth->getSidebar([
                'addon'     => ['new', 'red', 'badge'],
                'auth/rule' => __('Menu'),
                'zbtask' =>  ['待处理:'.$allcount , 'red', 'badge'],
                'zbtask/zbtask' => ['待审核:'.$task , 'red', 'badge'],
                'zbtask/money'   => ['待审核:'.	$zbtaskMoney , 'red', 'badge']
                ]);
        } else {
            //经销商
            list($menulist, $navlist, $fixedmenu, $referermenu) = $this->auth->getSidebar([
                'dashboard' => 'hot',
                'addon'     => ['new', 'red', 'badge'],
                'auth/rule' => __('Menu'),
            ], $this->view->site['fixedpage']);
        }
        $action = $this->request->request('action');
        if ($this->request->isPost()) {
            if ($action == 'refreshmenu') {
                $this->success('', null, ['menulist' => $menulist, 'navlist' => $navlist]);
            }
        }
        $this->view->assign('menulist', $menulist);
        $this->view->assign('navlist', $navlist);
        $this->view->assign('fixedmenu', $fixedmenu);
        $this->view->assign('referermenu', $referermenu);
        $this->view->assign('title', __('Home'));
        return $this->view->fetch();
    }

    /*定时任务更新数据*/
    public function refreshData() {
       if (TYPE == 0) {
           $zbtaskMoney = Money::where(['type' => 0, 'status'=> 1])->count(); //待审核众包费
           $noImport = Import::where(['is_import' => 0])->count(); //待审核众包费
           $task = Zbtask::where(['type' => 0, 'status'=> 1])->count();
           $allcount = $zbtaskMoney + $noImport + $task;

           $this->success('刷新', '',  [
               'wait_process' => '待处理:' . $allcount,
               'wait_check_task' => '待审核:' . $task,
               'wait_check_money' => '待审核:' . $zbtaskMoney. ',未导入:' .$noImport
           ]);
       }else if (TYPE == 1) {
           $com_where['company_id'] = TYPE_ID;
           $zbtaskMoney = Money::where(['type' => 0, 'status' => 1])->where($com_where)->count(); //待审核众包费
           $task = Zbtask::where(['type' => 0, 'status' => 1])->where($com_where)->count();
           $allcount = $zbtaskMoney + $task;
           $this->success('刷新', '',  [
               'wait_process' => '待处理:' . $allcount,
               'wait_check_task' => '待审核:' . $task,
               'wait_check_money' => '待审核:' . $zbtaskMoney
           ]);
       }

       $this->success('无数据');
    }

    /**
     * 管理员登录
     */
    public function login()
    {
        $url = $this->request->get('url', 'index/index');
        if ($this->auth->isLogin()) {
            $this->success(__("You've logged in, do not login again"), $url);
        }
        if ($this->request->isPost()) {
            $key = $this->request->post('key', '');
            $username = $this->request->post('username');
            $password = $this->request->post('password');
            $keeplogin = $this->request->post('keeplogin');
            $token = $this->request->post('__token__');
            $rule = [
                'username'  => 'require|length:3,30',
                'password'  => 'require|length:3,30',
                'key'  => 'require|length:3,30',
                '__token__' => 'require|token',
            ];
            $data = [
                'username'  => $username,
                'password'  => $password,
                'key'  => $key,
                '__token__' => $token,
            ];
            if (Config::get('fastadmin.login_captcha')) {
                $rule['captcha'] = 'require|captcha';
                $data['captcha'] = $this->request->post('captcha');
            }
            $validate = new Validate($rule, [], ['username' => __('Username'), 'password' => __('Password'), 'captcha' => __('Captcha')]);
            $result = $validate->check($data);
            if (!$result) {
                $this->error($validate->getError(), $url, ['token' => $this->request->token()]);
            }
            //获取总平台配置参数
            Platform::instance($key)->getPlatConfig();
            AdminLog::setTitle(__('Login'));
            $result = $this->auth->login($username, $password, $keeplogin ? 86400 : 0);
            if ($result === true) {
                Hook::listen("admin_login_after", $this->request);
                $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $username, 'avatar' => $this->auth->avatar]);
            } else {
                $msg = $this->auth->getError();
                $msg = $msg ? $msg : __('Username or password is incorrect');
                $this->error($msg, $url, ['token' => $this->request->token()]);
            }
        }

        // 根据客户端的cookie,判断是否可以自动登录
        if ($this->auth->autologin()) {
            $this->redirect($url);
        }
        $background = Config::get('fastadmin.login_background');
        $background = stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background;
        $this->view->assign('background', $background);
        $this->view->assign('title', __('Login'));
        Hook::listen("admin_login_init", $this->request);
        return $this->view->fetch();
    }

    /**
     * 注销登录
     */
    public function logout()
    {
        $this->auth->logout();
        Hook::listen("admin_logout_after", $this->request);
        $this->success(__('Logout successful'), 'index/login');
    }

}
